package 多线程04_死锁;

import 多线程01_启动线程.Hero;

/**
 * @ClassName: TestThreadDeadlock
 * @Author: AllenSun
 * @Date: 2022/3/18 下午10:42
 */
public class TestThreadDeadlock {
    public static void main(String[] args) {
        final Hero ahri = new Hero("九尾妖狐");
        final Hero annie = new Hero("安妮");

        Thread thread1 = new Thread() {
            public void run(){
                synchronized (ahri) {
                    System.out.println("thread1 已占有九尾妖狐");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread1 试图占有安妮");
                    System.out.println("thread1 等待中 。。。。");
                    //试图去占有
                    synchronized (annie) {
                        System.out.println("do something");
                    }
                }
            }
        };
        thread1.start();

        Thread thread2 = new Thread() {
            public void run(){
                synchronized (annie) {
                    System.out.println("thread1 已占有安妮");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread1 试图占有九尾妖狐");
                    System.out.println("thread1 等待中 。。。。");
                    //试图去占有
                    synchronized (ahri) {
                        System.out.println("do something");
                    }
                }
            }
        };
        thread2.start();


    }


}
